var userAjax;
var isLogged=false;
var userName ;
function loginListener(){
	var obj = validResponse(userAjax);
	if (obj != undefined){
		if(obj.loginResult == true){
			isLogged = true;
			userName = obj.userName;
			setLoggedin(obj.displayName);
			if(isViewItemPage()){
			fillViewItemPage();
			}else if(isHomePage()){
				getNewBids();
			}else{
				returnToHome();
			}
		}else{
			setUnLogged(false);
	}
	}
}


function checkAlreadyLoggedIn(){
	userAjax = fire("POST","Login",new Object(),loginListener);
}

function userLogin(params){
	userAjax = fire("POST","Login",params,loginListener);
}

function unauthorized(){
	setUnLogged(true);
}

//register methods 

function newUser(params){
	userAjax = fire("POST","Register",params,newUserCallback);
}

function newUserCallback(){
	var obj = validResponse(userAjax);
	if (obj != undefined){
		if(obj.result == true){
			returnToHome();
		}else{
			registrationError(obj.errorMessage);
		}
	}
}

function logout(){
	setUnLogged(false);
	fire("GET","Login",null,null);
	emptyCachedBids();
	isLogged = false;
}


setTimeout(function(){checkAlreadyLoggedIn();}, 20);
